# -*- coding: utf-8 -*-
# windows 下如果出现编码问题，将 utf-8 改为 cp936
import os

def findfile(key, inputdir='.'):
    found_list = []
    # os.walk 获取指定目录下的所有深度的文件、子目录的列表
    for path, dirnames, filenames in os.walk(inputdir):
        print 'searching', path, '...'
        for name in filenames:
            full_name = path + '/' + name
            if key in name:  # 如果文件名中有关键字
                found_list.append(full_name)
            with open(full_name) as f:
                for l in f.readlines():
                    if key in l:  # 如果当前行中有关键字
                        found_list.append(full_name + ' : ' + l)
    return found_list


# 输入搜索关键字和路径
keyword = raw_input('search:')
path = raw_input('in:')
if not path.strip():
    path = '.'

result = findfile(key, path)

print '\n========== result ===========\n\n'
for r in result:
    print r
